//
//  BookingDeleteController.m
//  whopaidforit
//
//  Created by Hoang Vu on 1/26/13.
//  Copyright (c) 2013 Hoang Vu. All rights reserved.
//

#import "BookingDeleteController.h"

@interface BookingDeleteController ()

@property(nonatomic,retain) NSMutableDictionary *keyArray;

@end

@implementation BookingDeleteController

@synthesize tblItems, btDelete, btCancel, listData, keyArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    listData = [[NSMutableArray alloc] init];
    keyArray = [[NSMutableDictionary  alloc]init];
    [self loadData];
    [self createNavigation];
    [self changeStyle];
}

-(void) loadData
{
    Event *event = [Event instance];
    Entry * entry = [Entry instance];
    self.listData = [entry getDatasWS:event._id];
    
    for(int i = 0 ; i< listData.count; i++)
    {
        [keyArray setObject:[NSNumber numberWithInt:0] forKey:[NSNumber numberWithInt:i]];
    }
}
-(void) changeStyle
{
    [StyleControl background:self.view];
    self.tblItems.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tblItems.backgroundColor = [UIColor clearColor];
    self.tblItems.backgroundView = nil;
    
    btCancel.layer.cornerRadius = 5;
    btDelete.layer.cornerRadius = 5;
    btCancel.layer.masksToBounds = YES;
    btDelete.layer.masksToBounds = YES;
}
-(void)createNavigation
{
    Event *event = [Event instance];
    UIButton *left =[UIButton buttonWithType:UIButtonTypeCustom];
    [left setImage:[UIImage imageNamed:@"buttonlist"] forState:UIControlStateNormal];
    left.frame = CGRectMake(8, 8, 37, 35);
    [left addTarget:self action:@selector(openListBooking) forControlEvents:UIControlEventTouchUpInside];
    
    [StyleControl  naviStyle:self.view andTitle:event._name andColor:0 andleftButton:left andRightButton:nil borderLeft:1 borderRight:1];
}
-(IBAction)cancelBooking:(id)sender
{
    [self openListBooking];
}
-(IBAction)deleteBooking:(id)sender
{
    int first = 1;
    NSMutableString * result = [[NSMutableString alloc] init];
    for(int i = 0 ; i< listData.count; i++)
    {
        BookingClass *par = [listData objectAtIndex:i];
        if( [[keyArray objectForKey:[NSNumber numberWithInt:par.bookingId]] integerValue] ){
            if(first){
                [result appendString:[NSString stringWithFormat:@"%d",par.bookingId] ];
                first = 0;
            }else{
                [result appendString:[NSString stringWithFormat:@"-%d",par.bookingId] ];
            }
        }
    }
    Entry *entry = [Entry instance];
    User *user = [User instance];
    [entry deleteListEntry:result userId:user._id];
    [self openListBooking];
}
-(void)openListBooking
{
    BookingListController *page=[[BookingListController alloc]init];
    [self presentModalViewController:page animated:YES];
    [page release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [listData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookingDeleteCell *cell;
    cell = [[[BookingDeleteCell alloc] initWithStyle:UITableViewCellSeparatorStyleNone reuseIdentifier:@"Cell"] autorelease];
    BookingClass *booking = [self.listData objectAtIndex:indexPath.row];
    ParticipateClass *par = [[ParticipateClass alloc] init];
    par.firstMail = booking.email;
    par.image = booking.picture;
    par.idString = booking.idString;
    cell.picture.image = [Helper getImageMember:par];
    cell.lblName.text = booking.bookingTitle;
    cell.lblDate.text = booking.bookingDate;
    cell.lblPrice.text = [StyleControl formatCurrency:[NSString stringWithFormat:@"%.2f",booking.bookingPrice] addCurency:YES];
    cell.lblQuantity.text = [NSString stringWithFormat:@"%dx", booking.bookingQuantity];
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(10, 15, 24, 24);
    button.tag=booking.bookingId;
    [button addTarget:self action:@selector(acceptPress:) forControlEvents:UIControlEventTouchUpInside];
    //-------------
    NSInteger value=[[keyArray objectForKey:[NSNumber numberWithInt:booking.bookingId]]integerValue ];
    if(value)
    {
        [button setImage:[UIImage imageNamed:@"deleteSelect"] forState:UIControlStateNormal];
    }
    else
    {
       [button setImage:[UIImage imageNamed:@"unDeleteSelect"] forState:UIControlStateNormal];
    }

    UIView *bgColorView = [[UIView alloc] init];
    [bgColorView setBackgroundColor:[UIColor clearColor]];
    [cell setSelectedBackgroundView:bgColorView];
    [bgColorView release];
        
    [cell addSubview:button];
    return cell;
}

-(void)counting
{
    NSInteger count =0;
    
    for(int i = 0 ; i<keyArray.count;i++)
    {
        if([[keyArray objectForKey:[NSNumber numberWithInt:i ]]integerValue ])
        {
            count++;
        }
    }
    [self.btDelete setTitle:[NSString stringWithFormat:@"Verwijder(%d)",count] forState:UIControlStateNormal];
    if (!count) {
        [self.btDelete setTitle:@"Verwijder" forState:UIControlStateNormal];
    }
}

-(IBAction)acceptPress:(id)sender
{
    UIButton *button=(UIButton *)sender;
    NSInteger value=[[keyArray objectForKey:[NSNumber numberWithInt:button.tag]]integerValue ];
    if(value)
    {
        [keyArray removeObjectForKey:[NSNumber numberWithInt:button.tag]];
        [keyArray setObject:[NSNumber numberWithInt:0] forKey:[NSNumber numberWithInt:button.tag]];
        [button setImage:[UIImage imageNamed:@"unDeleteSelect"] forState:UIControlStateNormal];
        [self counting];
    }
    else
    {
        [keyArray removeObjectForKey:[NSNumber numberWithInt:button.tag]];
        [keyArray setObject:[NSNumber numberWithInt:1] forKey:[NSNumber numberWithInt:button.tag]];
        [button setImage:[UIImage imageNamed:@"deleteSelect"] forState:UIControlStateNormal];
        
        [self counting];
    }
}

@end
